﻿#region << 版 本 注 释 >>
/*
 * ========================================================================
 * Copyright(c) 2008-2011深圳市宇商网科技有限公司, All Rights Reserved.
 * ========================================================================
 *  
 * 【当前类文件的功能】
 *  
 *  
 * 作者：[chao.jiang]   时间：2012-4-16 15:53:57
 * 文件名：RepositoryBase
 * 版本：V1.0.0
 * 
 * 修改者：           时间：               
 * 修改说明：
 * ========================================================================
*/
#endregion
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;
using System;

namespace Phoenix.Data.IBatis
{
    public abstract class RepositoryBase<T> : IRepository<T> where T : class
    {
        #region Properties
        protected ISqlMapper  dbContext { get; set; }

        #endregion

        #region Constructor
        public RepositoryBase()
		{
		}
        public RepositoryBase(string mapperName)
        {
            this.dbContext = SqlMapperManager.GetMapper(mapperName);
        }

        #endregion

        public T Get(object id)
        {
            string stmtId = GetStatementName("SelectByPK");
            return dbContext.QueryForObject<T>(stmtId, id);
        }

        public T GetAll()
        {
            string stmtId = GetStatementName("SelectAll");
            return dbContext.QueryForObject<T>(stmtId, null);
        }

        public object Save( T entity)
        {
            string stmtId = GetStatementName("Insert");
            return dbContext.Insert(stmtId, entity);
        }

        public void Update( T entity)
        {
            string stmtId = GetStatementName("Update");
            dbContext.Update(stmtId, entity);
        }

        public void Delete( object id)
        {
            string stmtId = GetStatementName("Delete");
            dbContext.Delete(stmtId, id);
        }

		#region Functions

		/// <summary>
		/// 得到运行时ibatis.net动态生成的SQL
		/// </summary>
		/// <param name="sqlMapper"></param>
		/// <param name="statementName"></param>
		/// <param name="paramObject"></param>
		/// <returns></returns>
		public virtual string GetRuntimeSql(ISqlMapper sqlMapper, string statementName, object paramObject)
		{
			string result = string.Empty;
			try
			{
				IMappedStatement statement = sqlMapper.GetMappedStatement(statementName);
				if (!sqlMapper.IsSessionStarted)
				{
					sqlMapper.OpenConnection();
				}
				RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, sqlMapper.LocalSession);
				result = scope.PreparedStatement.PreparedSql;
			}
			catch (Exception ex)
			{
				result = "获取SQL语句出现异常:" + ex.Message;
			}
			return result;
		}

		/// <summary>
		/// 获取sqlMap对应statement的完整id
		/// </summary> 
		/// <param name="name"></param>
		/// <returns></returns>
		protected virtual string GetStatementName(string name)
		{
			return string.Format("{0}.{1}", typeof(T).Namespace, name);
		}

		#endregion
    }
}



